package com.zou.lotterylucky

import android.annotation.SuppressLint
import android.content.Context
import androidx.multidex.MultiDexApplication
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import com.v2.utils.TApp
import com.zou.lotterylucky.data.BetEntity
import com.zou.lotterylucky.data.DealEntity
import com.zou.lotterylucky.data.IndexCountEntity
import com.zou.lotterylucky.data.MyObjectBox
import com.zou.lotterylucky.utils.AidlUtil
import com.zou.lotterylucky.utils.SPUtils
import io.objectbox.Box
import io.objectbox.BoxStore
import java.text.SimpleDateFormat
import java.util.*


class LotteryApp : TApp() {
    companion object {
        lateinit var context: Context

        lateinit var boxStore: BoxStore
        lateinit var dealBox: Box<DealEntity>
        lateinit var betBox: Box<BetEntity>
        lateinit var countBox: Box<IndexCountEntity>
        var appType = 1
        var offline = false

        fun getJiHao(context: Context): String {
            return SPUtils.get(context, "username", "001") as String
        }

        fun getXuhao(context: Context, time: String, i: Int): String {
//            val format = SimpleDateFormat("yyyyMMdd")
//            format.timeZone = TimeZone.getTimeZone("GMT-4")
            return getJiHao(context) + time + formatXuhao(i)
        }

        fun getDefaultTitle(context: Context) =
            if (appType == 1) "WEGA DI NUMBER KORSOU" else "WEGA DI NUMBER FLAMINGO"

        fun getTitle(zone: String) = "WEGA DI NUMBER\n" + when (zone) {
            "K" -> "KORSOU"
            "F" -> "FLAMINGO"
            "D" -> "DIARIO"
            "S" -> "S.T smart evening"
            else -> zone
        }

        fun getTitleSimple(zone: String) = when (zone) {
            "K" -> "KORSOU"
            "F" -> "FLAMINGO"
            "D" -> "DIARIO"
            "S" -> "S.T smart evening"
            else -> zone
        }

        private fun formatXuhao(index: Int): String {
            return String.format("%05d", index)
        }
    }

    @SuppressLint("CheckResult")
    override fun onCreate() {
        super.onCreate()
        context = this
        XLog.init(
            LogConfiguration.Builder()
                .logLevel(if (BuildConfig.DEBUG) LogLevel.ALL else LogLevel.NONE)
                .build()
        )
        appType = if (getString(R.string.apptype) == "1") 1 else 2
        offline = resources.getBoolean(R.bool.ofl)
        boxStore = MyObjectBox.builder().androidContext(this).build()
        dealBox = boxStore.boxFor(DealEntity::class.java)
        betBox = boxStore.boxFor(BetEntity::class.java)
        countBox = boxStore.boxFor(IndexCountEntity::class.java)

        val lang = SPUtils.get(this, Constants.SP_Lang, 1) as Int
        val configuration = resources.configuration
        when (lang) {
            -1, 0 -> configuration.setLocale(Locale("es", "CR"))
            1 -> configuration.setLocale(Locale.CHINESE)
            2 -> configuration.setLocale(Locale.ENGLISH)
        }

        resources.updateConfiguration(configuration, resources.displayMetrics)
        AidlUtil.instance.connectPrinterService(this)
    }


}